package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.PageData;
import cn.wolfcode.rbac.util.QueryConstanst;
import cn.wolfcode.rbac.util.R;
import cn.wolfcode.rbac.web.vo.RoleVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/roles")
public class RoleController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IRoleService roleService;
    @GetMapping
    public R query(QueryObject qo){
        PageInfo info = roleService.query(qo);
        return R.ok(new PageData(info));
    }
    @PostMapping
    public R add(@RequestBody RoleVo vo){
        roleService.add(vo);
        return R.ok();
    }
    @GetMapping("/{id}/permissions")
    public R queryPermissionByRoleId(@PathVariable("id") Long id){
        List<Permission> permissions =  permissionService.queryPermissionByRoleId(id);
        return R.ok(permissions);
    }
    @PutMapping("/{id}")
    public R update(@PathVariable Long id,@RequestBody RoleVo vo){
        roleService.update(id,vo);
        return R.ok();
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id){
        roleService.delete(id);
        return R.ok();
    }
    @GetMapping(headers = QueryConstanst.CMD_LIST)
    public R list(){
        List<Role> roles = roleService.listAll();
        return R.ok(roles);
    }
}
